package {
	
	import mx.styles.StyleManager;
	
	import vo.AvatarVO;
	import vo.GroupVO;
	import vo.ThemeVO;
	import vo.UserVO;
	
	[Bindable]
	public class UserSession {
		
		/** The ID allocated to this user session. */
		public static var sessionID:String;
		
		/** The personal details of the user. */
		public static var userData:UserVO;
		
		/** The groups the user has joined. */
		public static var groups:Array;
		
		/** The theme the user has chosen. */
		public static var theme:ThemeVO;
		
		/** The avatar the user has chosen. */
		public static var avatar:AvatarVO;
		
		/** Resets the application session to the logged out state. */
		public static function resetSession():void {
			
			var defaultTheme:ThemeVO = new ThemeVO();
			defaultTheme.themeName = "park";
			
			var defaultAvatar:AvatarVO = new AvatarVO();
			defaultAvatar.avatarName = "coder";
			
			sessionID = null;
			userData = null;
			groups = null;
			theme = defaultTheme;
			avatar = defaultAvatar;
			
			UserSession.loadStyles(defaultTheme, defaultAvatar);
		}
		
		/**
		 * Starts a new user session.
		 * @param sessionID The ID allocated to this user session.
		 * @param userData The personal details of the user.
		 * @param groups The groups the user has joined.
		 * @param theme The user's current theme to load.
		 * @param avatar The user's current avatar to load.
		 */
		public static function startSession(sessionID:String, userData:UserVO, groups:Array, theme:ThemeVO, avatar:AvatarVO):void {
			
			UserSession.sessionID = sessionID;
			UserSession.userData = userData;
			UserSession.groups = groups;
			UserSession.theme = theme;
			UserSession.avatar = avatar;
			
			UserSession.loadStyles(theme, avatar);
		}
		
		/**
		 * Set the application style settings.
		 * @param theme The theme to load.
		 * @param avatar The avatar to load. 
		 */
		public static function loadStyles(theme:ThemeVO, avatar:AvatarVO):void {
			
			UserSession.theme = theme;
			UserSession.avatar = avatar;
			StyleManager.loadStyleDeclarations("assets/themes/" + theme.themeName + ".swf");
		}
		
		/**
		 * Checks to see if the user is a member of a group.
		 * @param groupUID The group to check.
		 * @return True, if the user is a member of the group, otherwise false.
		 */
		public static function isMember(groupUID:String):Boolean {
			
			if(groups == null) {
				return false;
			}
			
			for(var i:int = 0; i < groups.length; i++) {
				if((groups[i] as GroupVO).groupUID == groupUID) {
					return true;
				}
			}
			return false;
		}
		
		/**
		 * Checks to see if the user is a manager of a group.
		 * @param groupUID The group to check.
		 * @return True, if the user is a manager of the group, otherwise false.
		 */
		public static function isManager(groupUID:String):Boolean {
			
			if(groups == null) {
				return false;
			}
			
			for(var i:int = 0; i < groups.length; i++) {
				
				var group:GroupVO = groups[i] as GroupVO;
				if(group.groupUID == groupUID && group.isManager) {
					return true;
				}
			}
			return false;
		}
	}
}